package com.yunshipei.utils;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.yunshipei.model.NotifyInfo;
import com.yunshipei.service.AlarmSeceiver;

/**
 * Created by cike on 15-11-19.
 */
public class AlarmSet {
    private AlarmManager alarmManager = null;
    private Intent intent;
    private PendingIntent sender;
    private Context context;

    public AlarmSet(Context context) {
        this.context = context;
        alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
    }

    /**
     * 设置一次性提醒
     *
     * @param alarmTime
     */
    public void setAlarm(NotifyInfo ni, long alarmTime) {
        setAlarm(ni, alarmTime, ni.get_id());
    }

    public void setAlarm(NotifyInfo ni, long alarmTime, int id) {
        intent = new Intent(context, AlarmSeceiver.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("speech", ni);
        intent.putExtra("speech1", bundle);
        sender = PendingIntent.getBroadcast(context, (int) (alarmTime + id), intent, PendingIntent.FLAG_UPDATE_CURRENT);
        alarmManager.set(AlarmManager.RTC_WAKEUP, alarmTime, sender);
    }

    /**
     * 设置重复提醒
     *
     * @param notifiInfo
     * @param repetionInterval 重复周期
     * @param alarmTime
     */
    public void setAlarm(NotifyInfo notifiInfo, int repetionInterval, long alarmTime) {
        setAlarm(notifiInfo, repetionInterval, alarmTime, notifiInfo.get_id());
    }
    /**
     * 设置重复提醒
     *
     * @param notifiInfo       通知信息
     * @param repetionInterval 重复周期
     * @param alarmTime        提醒时间
     * @param id               数据库标示id
     */
    public void setAlarm(NotifyInfo notifiInfo, int repetionInterval, long alarmTime, int id) {
        intent = new Intent(context, AlarmSeceiver.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("speech", notifiInfo);
        intent.putExtra("speech1", bundle);
        sender = PendingIntent.getBroadcast(context, (int) (alarmTime + id), intent, 0);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime, repetionInterval, sender);
        Toast.makeText(context, "提醒设置成功", Toast.LENGTH_SHORT).show();
    }

    public void cancelAlarm(NotifyInfo ni) {
        intent = new Intent(context, AlarmSeceiver.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("speech", ni);
        intent.putExtra("speech1", bundle);
        int requestCode = (int) (ni.getSpeechRecognition().getDateTime().getFormatDate() + ni.get_id());
        sender = PendingIntent.getBroadcast(context, requestCode, intent, 0);
        alarmManager.cancel(sender);
    }

    public void cancelAlarm(NotifyInfo ni, int notifiId) {
        intent = new Intent(context, AlarmSeceiver.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("speech", ni);
        intent.putExtra("speech1", bundle);
        sender = PendingIntent.getBroadcast(context, notifiId, intent, 0);
        alarmManager.cancel(sender);
    }

}
